package com.sky.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishFlavorService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 菜品业务层
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Resource
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增分类
     *
     * @param dishDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        // 属性拷贝
        BeanUtils.copyProperties(dishDTO,dish);
        // 设置创建时间、修改时间、创建人、修改人
        dish.setCreateTime(new Date());
        dish.setUpdateTime(new Date());
        dish.setCreateUser(BaseContext.getUserID());
        dish.setUpdateUser(BaseContext.getUserID());
        int insert = dishMapper.insert(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (CollectionUtil.isNotEmpty(flavors)) {
            dishFlavorService.inserBath(flavors, dish.getId());
        }

    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        Page<Object> result = PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        List<DishVO> dishes = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(result.getTotal(), dishes);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteByIds(List<Long> ids) {

        // 查询是否关联套餐
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(CollectionUtil.isNotEmpty(setmealIds)){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        for (Long id : ids) {
            //    根据id查询
            Dish dish = dishMapper.selectById(id);
            if (dish == null) {
                return Result.error("菜品不存在！");
            }
            if (dish.getStatus() == 0) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            dishMapper.deleteById(id);

        }
        return Result.success();
    }

    @Override
    public DishVO selectById(Long id) {
        DishVO dishVO = new DishVO();
        Dish dish = dishMapper.selectById(id);
        BeanUtils.copyProperties(dish,dishVO);

        List<DishFlavor> dishFlavors = dishFlavorService.selectByDishId(dish.getId());
        dishVO.setFlavors(dishFlavors);
        return dishVO ;
    }
}
